import {Basebio} from "./basebio";
import {Obj_class} from "./obj_class";
import { missile_actions} from "../commdata";
import {allobjects, ReleaseAllDestroidResource, ReStortObjects} from "../comm";

//
export class Missile extends Basebio{

    isSelect:boolean=false;
    img:any;
    imgDestroied:any=new Image();
    destoried_x:number=0;
    destoried_y:number=0;
    destoried_height:number=100;
    destoried_width:number=100;
    constructor(_name:string) {
        super();
        this.name = _name;
        this.img=new Image();
        this.actrons=missile_actions;
        this.img.src=this.actrons[0].imgsrc;
        this.imgDestroied.src=this.actrons[1].imgsrc;
        this.ratewidth = this.img.width/2;
        this.rateheight = this.img.height/2;
        this.destoried_height=this.imgDestroied.height-this.imgDestroied.height/4;
        this.destoried_width=this.imgDestroied.width-this.imgDestroied.width/4;
        this.demage=300;
        this.isSelect=false;
    }

    missileDestroyDraw=(ptctx: any,_x:number,_y:number)=>{

        ptctx.drawImage(this.imgDestroied,0,0,this.imgDestroied.width,this.imgDestroied.height,
            _x-this.destoried_width/2-2,
            _y-this.destoried_height/2-2,this.destoried_width,this.destoried_height);
    }


    missiledraw=(ptctx: any)=>{

       // ptctx?.drawImage(this.img, 0, 0,this.img.width,this.img.height,this.x, this.y, this.ratewidth, this.rateheight);

        if(this.isSelect==true) {
            ptctx.beginPath();
            ptctx.strokeStyle="green";
            ptctx.clearRect(this.x-2, this.y, this.ratewidth+2, this.rateheight+2);
            ptctx.rect(this.x-2, this.y, this.ratewidth+2, this.rateheight+2);
            ptctx.stroke();
            ptctx.drawImage(this.img, 1, 1, this.img.width, this.img.height, this.x, this.y, this.ratewidth, this.rateheight);
        }
        else {
            ptctx.drawImage(this.img, 0, 0, this.img.width, this.img.height, this.x, this.y, this.ratewidth, this.rateheight);
        }

    }

    destroyed=(objs:Obj_class[])=>{
        for(let i=0;i<objs.length;i++){
            let enemy:Basebio=objs[i].objval;
            if(enemy.isstart==true && enemy.enable==true && enemy.live>0) {
                if (enemy.x > this.destoried_x - this.destoried_width && enemy.x < this.destoried_x + this.destoried_width) {
                    if (enemy.y > this.destoried_y - this.destoried_height && enemy.y < this.destoried_y + this.destoried_height) {
                        enemy.live = enemy.live - this.demage;
                        if (enemy.live <= 0) {
                            enemy.live = 0;
                        }
                    }
                }
            }
        }
    }
}